package com.iexbuy.ihg.utils;

import android.util.Log;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONStringer;

import com.gson.Gson;
import com.gson.JsonArray;
import com.iexbuy.ihg.entity.LingQianLastestObject;

public class JsonUtils {
	//private final static String TAG="JsonUtils";
	public static <T> T  parseJsonStrToObject(String jsonData,Class<T> classOfT){
		Gson gson=new Gson();
		return gson.fromJson(jsonData, classOfT);
	}

	public static <T> T  parseJsonStrToObject(String jsonData, Type type){
		Gson gson=new Gson();
		return gson.fromJson(jsonData, type);
	}
	
	public static <T> T readJsonObjectFormFile(String filePath,Class<T> classOfT) throws JSONException, IOException{
		String jsonData=FileUtils.readToStringFormFile(filePath);
		Gson gson=new Gson();
		return gson.fromJson(jsonData, classOfT);
	}
	
	
	public static <T> T readJsonObjectFormNet(String filePath,Class<T> classOfT) throws IOException{
		String jsonStr=FileUtils.readToStringFormFile(filePath);
		Gson gson=new Gson();
		return gson.fromJson(jsonStr, classOfT);
	}
	
	public static String ObjectToJson(Object object){
		Gson gson=new Gson();
		String json = gson.toJson(object);
		return json;  
	}


	public static String getJsonSubStringByKey(String jsonStr, String key) {
		String subString = null;
		try {
			JSONObject dataJson=new JSONObject(jsonStr);
			subString = dataJson.getString(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return subString;
	}

	public static JSONObject getJsonObjectByKey(String jsonStr, String key) {
		JSONObject jSONObject = null;
		try {
			JSONObject dataJson=new JSONObject(jsonStr);
			jSONObject = dataJson.getJSONObject(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jSONObject;
	}
	public static Map<String,String> getJsonByKey(String jsonStr, String body) {

		Map<String,String> map = new HashMap<String, String>();
		JSONObject obj = null;
		try {
			obj = new JSONObject(jsonStr);
			JSONObject objData = obj.getJSONObject(body);
			Iterator<String> keys = objData.keys();
			while (keys.hasNext()){
				String key=  keys.next();
				map.put(key,objData.getString(key));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return map;
	}
}
